package com.grf.es.highlevelrest;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RestHighLevelClient;

public class Bulk {
	
	/**
	 * 主函数
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		Bulk bulk = new Bulk();
		bulk.bulkIndex();
		//bulk.bulkUpdate();
	}
	
	/**
	 * 批量添加文档 
	 * @throws IOException 
	 */
	public void bulkIndex() throws IOException {
		RestHighLevelClient client = Client.getClient();
		Map<String, Object> map = new HashMap();
		map.put("userName", "bulk test");
		map.put("id", "11");
		Map<String, Object> map1 = new HashMap();
		map1.put("userName", "仗义");
		map1.put("id", "12");
		Map<String, Object> map2 = new HashMap();
		map1.put("userName", "说话");
		map1.put("id", "13");
		BulkRequest bulkRequest = new BulkRequest();
		bulkRequest.add(new IndexRequest("test", "user")
				.source(map)
				.source(map1)
				.source(map2)
				);
		BulkResponse bulkResponse = client.bulk(bulkRequest);
		showInfo(bulkResponse);
	}
	
	/**
	 *  批量更新 
	 * @throws IOException 
	 */
	public void bulkUpdate() throws IOException {
		Map<String, Object> map = new HashMap();
		map.put("userName", "bulk test1");
		map.put("id", "12");
		RestHighLevelClient client = Client.getClient();
		BulkRequest bulkRequest = new BulkRequest();
		UpdateRequest updateRequest = new UpdateRequest("test", "user", "AW6bTLvaRSi-EJPlOidk");
		bulkRequest.add(updateRequest.doc(map));
		BulkResponse bulkResponse = client.bulk(bulkRequest);
		showInfo(bulkResponse);
	}
	
	/**
	 * 批量获取
	 * @throws IOException
	 */
	public void bulkGet() throws IOException {
		RestHighLevelClient client = Client.getClient();
		GetRequest getRequest = new GetRequest("test", "user", "id");
		
		
		GetResponse getResponse = client.get(getRequest);
		getResponse.getSource();
		//getRequest.fetchSourceContext()
	}
	
	/**
	 * 显示操作结果
	 */
	public void showInfo(BulkResponse bulkResponse) {
		if (null != bulkResponse) {
			for (BulkItemResponse bulkItemResponse: bulkResponse) {
				DocWriteResponse itemResponse = bulkItemResponse.getResponse();
				
				if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.CREATE
						|| bulkItemResponse.getOpType() == DocWriteRequest.OpType.INDEX) {
					System.out.println("新增成功");
				} else if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.UPDATE) {
					System.out.println("更新成功");
				} else if(bulkItemResponse.getOpType() == DocWriteRequest.OpType.DELETE) {
					System.out.println("删除成功");
				}
			}
		}
	}
	
}
